package main

import (
	"fmt"

	"gitee.com/codays/go-fiber-gorm-sqlite-poc/controller/book"
	"gitee.com/codays/go-fiber-gorm-sqlite-poc/db/conn"

	"github.com/gofiber/fiber"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

func setupRoutes(app *fiber.App) {
	app.Get("/api/v1/book", book.GetBooks)
	app.Get("/api/v1/book/:id", book.GetBook)
	app.Post("/api/v1/book", book.NewBook)
	app.Delete("/api/v1/book/:id", book.DeleteBook)
}

func init() {
	var err error
	conn.SqliteCon, err = gorm.Open(sqlite.Open("book.db"), &gorm.Config{})
	if err != nil {
		panic("failed to connect dabase")
	}
	fmt.Println("Database connection succesfully opened")
	conn.SqliteCon.AutoMigrate(&book.Book{})
	fmt.Println("Database Migrated")
}

func main() {
	app := fiber.New()

	setupRoutes(app)

	app.Listen(3000)
}
